package com.baiwei.vpnapi.handler;

import com.baiwei.vpnapi.exception.*;
import com.baiwei.vpnapi.utils.ResultVoUtils;
import com.baiwei.vpnapi.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.xml.transform.Result;

/**
 * @program: vpnapi
 * @description: 错误处理
 * @author: Mr. Stone
 * @create: 2020-02-26 14:11
 **/
@ControllerAdvice
public class ErrorController extends ResponseEntityExceptionHandler {

    @Autowired MessageSourceHandler messageSourceHandler;

    private Logger logger = LoggerFactory.getLogger(ErrorController.class);

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object handleRuntimeError(RuntimeException e) {
        logger.info(e.getMessage());
        e.printStackTrace();
        return ResultVoUtils.fail(e.getMessage(), 10010);
    }
    @ExceptionHandler(AccountException.class)
    @ResponseBody
    public Object handleAccountException(AccountException e) {
        logger.info(e.getMessage());
        return ResultVoUtils.fail(messageSourceHandler.getMessage(e.getMessage()), e.getCode());
    }
    @ExceptionHandler(AuthException.class)
    @ResponseBody
    public Object handleAuthException(AuthException e) {
        logger.info(e.getMessage());
        return ResultVoUtils.fail(messageSourceHandler.getMessage(e.getMessage()), e.getCode());
    }
    @ExceptionHandler(VpnException.class)
    @ResponseBody
    public Object handleVpnException(VpnException e) {
        logger.info(e.getMessage());
        return ResultVoUtils.fail(messageSourceHandler.getMessage(e.getMessage()), e.getCode());
    }
    @ExceptionHandler(PaymentException.class)
    @ResponseBody
    public Object handlePaymentException(PaymentException e) {
        logger.info(e.getMessage());
        return ResultVoUtils.fail(messageSourceHandler.getMessage(e.getMessage()), e.getCode());
    }
    @ExceptionHandler(AdminException.class)
    @ResponseBody
    public Object handleAdminException(AdminException e) {
        logger.info(e.getMessage());
        return ResultVoUtils.fail(messageSourceHandler.getMessage(e.getMessage()), e.getCode());
    }
}

